package com.occludens.server.dispatch;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.code.twig.ObjectDatastore;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.shared.ActionException;
import com.occludens.shared.dispatch.GetUserAction;
import com.occludens.shared.dispatch.GetUserResult;
import com.occludens.shared.model.UserInfo;

public class GetUserHandler extends	DispatchActionHandler<GetUserAction, GetUserResult> {

	@Inject
	//public GetUserHandler(final Provider<UserInfo> userInfoProvider, final ObjectDatastore datastore) {
	public GetUserHandler(final Provider<UserInfo> userInfoProvider) {
		//super(userInfoProvider, datastore);
		super(userInfoProvider);
	}

	@Override
	public GetUserResult execute(GetUserAction action, ExecutionContext context)
			throws ActionException {

		UserInfo userInfo = userInfoProvider.get();

		UserService userService = UserServiceFactory.getUserService();

		if (userInfo.isSignedIn) {
			userInfo.signOutURL = userService.createLogoutURL(action
					.getRequestURI());

		} else {
			userInfo.signInURLs.put("Google", userService.createLoginURL(
					action.getRequestURI(), null, "google.com/accounts/o8/id",
					null));
			userInfo.signInURLs.put("Yahoo", userService.createLoginURL(
					action.getRequestURI(), null, "yahoo.com", null));
		}

		return new GetUserResult("", userInfo);
	}
}